
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>添加函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="添加数据表.html" />
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../快速使用.html">
            
                <a href="../快速使用.html">
            
                    
                    快速使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../函数及常量.html">
            
                <a href="../函数及常量.html">
            
                    
                    函数及常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../BARRA因子.html">
            
                <a href="../BARRA因子.html">
            
                    
                    因子计算
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../已支持的表.html">
            
                <a href="../已支持的表.html">
            
                    
                    已支持的表
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../balance_sheet.html">
            
                <a href="../balance_sheet.html">
            
                    
                    balance_sheet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../deriv_finance_indicator.html">
            
                <a href="../deriv_finance_indicator.html">
            
                    
                    deriv_finance_indicator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../fundamentals_balance.html">
            
                <a href="../fundamentals_balance.html">
            
                    
                    fundamentals_balance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../fundamentals_cashflow.html">
            
                <a href="../fundamentals_cashflow.html">
            
                    
                    fundamentals_cashflow
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../fundamentals_income.html">
            
                <a href="../fundamentals_income.html">
            
                    
                    fundamentals_income
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../share_change.html">
            
                <a href="../share_change.html">
            
                    
                    share_change
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../trading_derivative_indicator.html">
            
                <a href="../trading_derivative_indicator.html">
            
                    
                    trading_derivative_indicator
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../项目说明.html">
            
                <a href="../项目说明.html">
            
                    
                    项目说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    参与项目开发
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.7.1" data-path="添加函数.html">
            
                <a href="添加函数.html">
            
                    
                    添加函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="添加数据表.html">
            
                <a href="添加数据表.html">
            
                    
                    添加数据表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >添加函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6DFB;&#x52A0;&#x51FD;&#x6570;">&#x6DFB;&#x52A0;&#x51FD;&#x6570;</h1>
<p>&#x975E;&#x5E38;&#x5EFA;&#x8BAE;&#x5C06;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x5230;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6B65;&#x9AA4;&#x53C8;&#x6216;&#x5E38;&#x91CF;&#xFF0C;&#x5C01;&#x88C5;&#x6210;&#x51FD;&#x6570;&#x5E76;&#x52A0;&#x5165;&#x672C;&#x9879;&#x76EE;&#xFF0C;&#x901A;&#x8FC7;<code>pull</code>&#x7684;&#x65B9;&#x5F0F;&#x3002;&#x4E0D;&#x8FC7;&#x60A8;&#x9700;&#x8981;&#x6309;&#x7167;&#x5982;&#x4E0B;&#x51E0;&#x4E2A;&#x6B65;&#x9AA4;&#x8FDB;&#x884C;&#xFF1A;</p>
<h2 id="&#x786E;&#x8BA4;&#x57FA;&#x672C;&#x60C5;&#x51B5;">&#x786E;&#x8BA4;&#x57FA;&#x672C;&#x60C5;&#x51B5;</h2>
<ul>
<li>&#x51FD;&#x6570;&#x7C7B;&#x578B;&#xFF1A;&#x51FD;&#x6570;&#x5C5E;&#x4E8E;&#x56DE;&#x6D4B;&#x76F8;&#x5173;&#x8FD8;&#x662F;&#x6570;&#x636E;&#x5904;&#x7406;&#x76F8;&#x5173;&#xFF0C;&#x8FD9;&#x5C06;&#x51B3;&#x5B9A;&#x51FD;&#x6570;&#x52A0;&#x5165;&#x54EA;&#x4E2A;&#x6587;&#x4EF6;</li>
<li>&#x517C;&#x5BB9;&#x6027;&#xFF1A;&#x662F;&#x5426;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x4F1A;&#x6709;&#x5176;&#x4ED6;&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x4F8B;&#x5982;&#x65E5;&#x671F;&#x53EF;&#x80FD;&#x4F1A;&#x662F;<code>str&#x3001;int&#x3001;datetime</code>&#xFF0C;&#x53D8;&#x91CF;&#x82E5;&#x4E0D;&#x5982;&#x4F60;&#x7684;&#x9884;&#x671F;&#xFF0C;&#x51FD;&#x6570;&#x8FD8;&#x80FD;&#x6267;&#x884C;&#x5417;&#xFF1F;</li>
<li><p><code>Error</code>&#x5904;&#x7406;&#xFF1A;<code>Error</code>&#x662F;&#x5728;&#x6240;&#x96BE;&#x514D;&#x7684;&#xFF0C;&#x51FD;&#x6570;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x54EA;&#x4E9B;&#x62A5;&#x9519;&#xFF0C;&#x662F;&#x5426;&#x4F7F;&#x7528;<code>Raise Error</code>&#x8BED;&#x53E5;&#x4F7F;&#x5F97;&#x4F7F;&#x7528;&#x8005;&#x62A5;&#x9519;&#x7B80;&#x5355;&#x660E;&#x4E86;&#xFF1F;</p>
</li>
<li><p>&#x5F53;&#x7136;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x60F3;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x7EC6;&#x8282;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x4F60;&#x7684;&#x9700;&#x6C42;&#x544A;&#x8BC9;&#x6211;&#xFF0C;&#x6211;&#x5E2E;&#x52A9;&#x4F60;&#x5B9E;&#x73B0;&#x6B64;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4F9D;&#x65E7;&#x611F;&#x8C22;&#x60A8;&#x505A;&#x51FA;&#x7684;&#x8D21;&#x732E;&#xFF01;</p>
</li>
</ul>
<h2 id="&#x6DFB;&#x52A0;&#x51FD;&#x6570;">&#x6DFB;&#x52A0;&#x51FD;&#x6570;</h2>
<p>&#x4EE5;<code>format_date</code>&#x4E3A;&#x4F8B;&#xFF0C;&#x6B64;&#x51FD;&#x6570;&#x7528;&#x4E8E;&#x5C06;&#x5404;&#x79CD;&#x683C;&#x5F0F;(&#x5305;&#x62EC;<code>int&#xFF0C;str&#xFF0C;datetime</code>)&#x7684;&#x65E5;&#x671F;&#x8F6C;&#x5316;&#x4E3A;<code>pd.datetime</code>&#x683C;&#x5F0F;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">format_date</span><span class="hljs-params">(date)</span>:</span>
    <span class="hljs-keyword">if</span> isinstance(date, datetime.datetime):
        <span class="hljs-keyword">return</span> date
    <span class="hljs-keyword">elif</span> isinstance(date, int):
        date = pd.to_datetime(date, format=<span class="hljs-string">&apos;%Y%m%d&apos;</span>)
        <span class="hljs-keyword">return</span> date
    <span class="hljs-keyword">elif</span> isinstance(date, str):
        date = pd.to_datetime(date)
        <span class="hljs-keyword">return</span> date
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">&quot;date should be str or int!&quot;</span>)
</code></pre>
<ul>
<li>&#x6B64;&#x51FD;&#x6570;&#x5BF9;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x7684;&#x53EF;&#x80FD;<code>type</code>&#x8FDB;&#x884C;&#x4E86;&#x517C;&#x5BB9;&#xFF0C;&#x5305;&#x62EC;<code>int&#xFF0C;str&#xFF0C;datetime</code>&#xFF0C;&#x5BF9;&#x4E0D;&#x540C;&#x7684;&#x683C;&#x5F0F;&#x8F6C;&#x5316;&#x6210;<code>pd.datetime</code>&#x6709;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x5F0F;</li>
<li>&#x5BF9;&#x4E8E;&#x610F;&#x5916;&#x7684;<code>type</code>&#x8FDB;&#x884C;&#x4E86;&#x62A5;&#x9519;&#x5904;&#x7406;&#xFF0C;&#x5E76;&#x629B;&#x51FA;&#x5BF9;&#x5E94;&#x7684;&#x63D0;&#x793A;&#x4FE1;&#x606F;</li>
</ul>
<h2 id="&#x52A0;&#x5165;utilspy">&#x52A0;&#x5165;<code>Utils.py</code></h2>
<p>&#x5C06;&#x51FD;&#x6570;&#x5185;&#x5BB9;&#x52A0;&#x5165;&#x5230;<code>Utils.py</code>&#x4E2D;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x5728;<code>__all__</code>&#x4E2D;&#x52A0;&#x5165;&#x51FD;&#x6570;&#x540D;&#xFF0C;&#x8FD9;&#x4E2A;&#x5E38;&#x91CF;&#x50A8;&#x5B58;&#x4E86;&#x53EF;&#x4EE5;&#x88AB;<code>from Utils import *</code>&#x6240;&#x80FD;&#x5F15;&#x5165;&#x7684;&#x6240;&#x6709;&#x5BF9;&#x8C61;</p>
<p>&#x6700;&#x540E;&#x8BB0;&#x5F97;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;&#x770B;&#x8BE5;&#x51FD;&#x6570;&#x662F;&#x5426;&#x8FBE;&#x5230;&#x4E86;&#x9884;&#x671F;&#x6548;&#x679C;&#x3002;&#x4E25;&#x7981;&#x5C06;&#x672A;&#x6D4B;&#x8BD5;&#x6210;&#x529F;&#x7684;&#x51FD;&#x6570;&#x52A0;&#x5165;&#x9879;&#x76EE;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="添加数据表.html" class="navigation navigation-next navigation-unique" aria-label="Next page: 添加数据表">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"添加函数","level":"1.7.1","depth":2,"next":{"title":"添加数据表","level":"1.7.2","depth":2,"path":"参与项目开发/添加数据表.md","ref":"参与项目开发/添加数据表.md","articles":[]},"previous":{"title":"参与项目开发","level":"1.7","depth":1,"ref":"","articles":[{"title":"添加函数","level":"1.7.1","depth":2,"path":"参与项目开发/添加函数.md","ref":"参与项目开发/添加函数.md","articles":[]},{"title":"添加数据表","level":"1.7.2","depth":2,"path":"参与项目开发/添加数据表.md","ref":"参与项目开发/添加数据表.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"参与项目开发/添加函数.md","mtime":"2023-04-15T08:47:05.331Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2023-07-01T05:15:14.464Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

